package com.cheng.io.output;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 构造器： FileOutputStream(String name) 写入文件到指定路径，写入前会先清空文件中的数据
*         FileOutputStream(String name, boolean append) 写入文件到指定路径，如果append值为true则不会清空数据在原数据的基础上追加数据
 *
 * 方法：
 *      void close(); 关闭流
 *      void flush(); 刷新管道中的数据，强制剩余数据写入到文件中
 *      void write(int b); 写入一个字节，参数为ASCII码
 *      void write(byte b[]); 写入byte数组数量的字节
 *      void write(byte b[], int off, int len); 写入从byte数组中的off下标开始到len数量结束
 *
 */
public class FileOutputStreamTest01 {

    public static void main(String[] args) {
        OutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream("D:\\code\\java-study\\java-study\\io\\resources\\test02.txt",true);
            outputStream.write(97);
            outputStream.write(97);
            outputStream.write(97);


            byte[] bytes = new byte[]{98,99,100,101};
            outputStream.write(bytes);
            outputStream.write(bytes,1,2);

            outputStream.flush();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (outputStream != null) {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
